package cc.rengu.oltp.service.common.dao.impl;

import cc.rengu.jradp.mods.xml.XmlTree;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.dao.RspCodeMapMapper;
import cc.rengu.oltp.service.common.entity.RspCodeMap;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 业务应答码表操作接口实现类
 * Created by 王成 on 2018/4/18.
 */
public class RspCodeMapMapperImpl implements RspCodeMapMapper {
    private final RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppParamConstant.DATABASE_CONF_NODE, AppParamConstant.OLTP_POOL_NAME);

    public RspCodeMapMapperImpl() throws Exception {
        if (XmlTree.hasThreadTree()) {
            XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
            String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
            if (null != corporation && !corporation.isEmpty()) {
                String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
                dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppParamConstant.OLTP_POOL_NAME);
            }
        }
    }

    @Override
    public void loadAllRespCodeMapInfo(Set<String> dbPoolCashNode, List<RspCodeMap> rspCodeMapList) throws Exception {
        for (String dbCaseNodeName : dbPoolCashNode) {
            String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbCaseNodeName, AppParamConstant.OLTP_POOL_NAME);
            DbsUtil dbsUtil = new DbsUtil(dbPoolName);
            String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "loadAllRespCodeMapInfo");
            List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, null, RspCodeMap.class);
            if (objectList.isEmpty()) {
                rglog.warn("获取数据库连接池【{}】全量业务响应码映射关系失败!", dbPoolName);
            } else {
                rspCodeMapList.addAll(objectList.stream().map(item -> (RspCodeMap) (item)).collect(Collectors.toList()));
            }
        }
    }

    @Override
    public void loadAllRespCodeMapInfo(List<RspCodeMap> rspCodeMapList) throws Exception {
        String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppParamConstant.DATABASE_CONF_NODE, AppParamConstant.OLTP_POOL_NAME);
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "loadAllRespCodeMapInfo");
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, null, RspCodeMap.class);
        if (objectList.isEmpty()) {
            rglog.warn("获取数据库连接池【{}】全量业务响应码映射关系失败!", dbPoolName);
        } else {
            rspCodeMapList.addAll(objectList.stream().map(item -> (RspCodeMap) (item)).collect(Collectors.toList()));
        }
    }

    @Override
    public List<RspCodeMap> selectRespCodeMapInfoByInstId(String instId) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectRespCodeMapInfoByInstId");
        RspCodeMap rspCodeMap = new RspCodeMap();
        rspCodeMap.setInstId(instId);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, rspCodeMap, RspCodeMap.class);
        if (objectList.isEmpty()) {
            rglog.warn("根据多法人标识<{}>获取所有错误码映射信息失败!", instId);
            return null;
        }
        return objectList.stream().map(item -> (RspCodeMap) (item)).collect(Collectors.toList());
    }

    @Override
    public List<RspCodeMap> selectRespCodeMapInfoBySrcRspCode(String instId, String srcSysId, String srcRspCode) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectRespCodeMapInfoBySrcRspCode");
        RspCodeMap rspCodeMap = new RspCodeMap();
        rspCodeMap.setInstId(instId);
        rspCodeMap.setSrcSysId(srcSysId);
        rspCodeMap.setSrcRspCode(srcRspCode);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, rspCodeMap, RspCodeMap.class);
        if (objectList.isEmpty()) {
            rglog.warn("根据多法人标识<{}>获取所有错误码映射信息失败!", instId);
            return null;
        }
        return objectList.stream().map(item -> (RspCodeMap) (item)).collect(Collectors.toList());
    }

    @Override
    public RspCodeMap selectRespCodeMapInfoByPrimaryKey(String instId, String srcSysId, String srcRspCode, String rspCodeScene, String dstSysId, String dstChannelId) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectRespCodeMapInfoByPrimaryKey");
        RspCodeMap rspCodeMap = new RspCodeMap();
        rspCodeMap.setInstId(instId);
        rspCodeMap.setSrcSysId(srcSysId);
        rspCodeMap.setSrcRspCode(srcRspCode);
        rspCodeMap.setRspCodeScene(rspCodeScene);
        rspCodeMap.setDestSysId(dstSysId);
        rspCodeMap.setDestChannelId(dstChannelId);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, rspCodeMap, rspCodeMap);
        if (0 != iReturnCode) {
            rglog.warn("根据主键查询错误码映射关系失败,instId:<{}>,srcSysId:<{}>,srcRspCode:<{}>,dstSysId:<{}>,iReturnCode:<{}>", instId, srcSysId, srcRspCode, dstSysId, iReturnCode);
            return null;
        }
        return rspCodeMap;
    }
}
